package cn.iocoder.yudao.framework.common.util.alibaba;

import cn.iocoder.yudao.framework.common.pojo.AlibabaIDAuthenticationResult;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;
@Slf4j
public class IdAuthenticationUtils {
    static String host = "https://mobilecert.market.alicloudapi.com";
    static String path = "/mobile3Meta";
    static String method = "GET";
    static String appcode = "d768e9343f374a5aa70ba3deb0135cf9";
    public static Map<String,Object> idAuthentication(String identifyNum,String mobile,String userName){
        Map<String, Object> resp = new HashMap<String, Object>();
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("identifyNum", identifyNum);
//        querys.put("identifyNumMd5", "identifyNumMd5");
        querys.put("mobile", mobile);
//        querys.put("mobileMd5", "mobileMd5");
        querys.put("userName", userName);
//        querys.put("userNameMd5", "userNameMd5");
        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = AlibabaHttpsUtils.doGet(host, path, method, headers, querys);
            //获取response的body
            AlibabaIDAuthenticationResult alibabaIDAuthenticationResult= JsonUtils.parseObject(EntityUtils.toString(response.getEntity()), AlibabaIDAuthenticationResult.class);
            //获取response的body
            log.info("三要素验证参数,identifyNum:"+identifyNum+",mobile:"+mobile+",userName:"+userName);
            log.info("三要素验证返回:"+alibabaIDAuthenticationResult.toString());
            if(alibabaIDAuthenticationResult.getCode().equals("200")) {
                if (alibabaIDAuthenticationResult.getData() != null) {
                    if (alibabaIDAuthenticationResult.getData().getBizCode().equals("1")) {
                        if(alibabaIDAuthenticationResult.getData().getSubCode().equals("101")){
                            resp.put("code","1");
                            resp.put("message","验证通过");
                            return resp;
                        }
                    } else if(alibabaIDAuthenticationResult.getData().getBizCode().equals("2")) {
                        resp.put("code","2");
                        if(alibabaIDAuthenticationResult.getData().subCode.equals("301")){
                            resp.put("message","查⽆记录");
                        }
                        if(alibabaIDAuthenticationResult.getData().subCode.equals("201")){
                            resp.put("message","手机号和姓名不一致，手机号和证件号不一致");
                        }
                        if(alibabaIDAuthenticationResult.getData().subCode.equals("202")){
                            resp.put("message","⼿机号和姓名⼀致，⼿机号和证件号不⼀致");
                        }
                        if(alibabaIDAuthenticationResult.getData().subCode.equals("203")){
                            resp.put("message","⼿机号和证件号⼀致，⼿机号和姓名不⼀致");
                        }
                        if(alibabaIDAuthenticationResult.getData().subCode.equals("204")){
                            resp.put("message","其他不⼀致");
                        }
                        return resp;
                    }else if(alibabaIDAuthenticationResult.getData().getBizCode().equals("3")) {
                        resp.put("code","3");
                        resp.put("message","审核资料格式不正确");
                        return resp;
                    }
                }
            }
            log.error("三要素验证code:",alibabaIDAuthenticationResult.getCode());
        } catch (Exception e) {
            log.error("三要素验证异常",e);
            resp.put("code","2");
            resp.put("flag",false);
        }
        resp.put("code","4");
        resp.put("message","查询异常");
        return resp;
    }


}
